﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
using Ahi.Base;
using Ahi.Entity;
using DevExpress.XtraReports.Design;
using Ahi.Entity.Interfaces;

namespace Ahi.Raporlama
{
    public partial class RaporTasarimForm : BaseForm
    {
        Type dataSourceType = null;

        public RaporTasarimForm()
        {
            InitializeComponent();
            IsMdiContainer = false;
        }

        public override void Yukle()
        {
            if (Id.HasValue)
            {
                KayitYukle(Id);
            }
            else
            {
                YeniKayit();
            }
        }

        public void YeniKayit()
        {
            xrDesignPanel1.AddCommandHandler(new SaveCommandHandler(xrDesignPanel1, new IdFormParameter(), dataSourceType));
            xrDesignPanel1.AddCommandHandler(new OpenCommandHandler(xrDesignPanel1, new IdFormParameter()));

            xrDesignPanel1.ExecCommand(DevExpress.XtraReports.UserDesigner.ReportCommand.NewReport);
        }

        public void KayitYukle(IdFormParameter Id)
        {
            xrDesignPanel1.AddCommandHandler(new SaveCommandHandler(xrDesignPanel1, Id, dataSourceType));
            xrDesignPanel1.AddCommandHandler(new OpenCommandHandler(xrDesignPanel1, Id));

            xrDesignPanel1.ExecCommand(DevExpress.XtraReports.UserDesigner.ReportCommand.OpenFile);
        }

        private void buttonAddDataSource_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetDataSource();
        }

        private void SetDataSource()
        {
            BindingSource bs = new BindingSource();
            if (EntitySecimForm<IRaporlanabilir>.ShowEntitySecim(ref dataSourceType) == DialogResult.OK)
            {
                bs.DataSource = dataSourceType;
                if (Id.HasValue)
                    xrDesignPanel1.AddCommandHandler(new SaveCommandHandler(xrDesignPanel1, Id, dataSourceType));
                else
                    xrDesignPanel1.AddCommandHandler(new SaveCommandHandler(xrDesignPanel1, new IdFormParameter(), dataSourceType));

                try
                {
                    foreach (var IComp in xrDesignPanel1.Report.Container.Components)
                    {
                        var ctl = IComp as Component;
                        if (ctl != null && ctl.ToString().Contains("bindingSource1"))
                        {
                            xrDesignPanel1.Report.Container.Remove(ctl);
                            break;
                        }
                    }

                    xrDesignPanel1.Report.Container.Add(bs, "bindingSource1");
                    xrDesignPanel1.Report.DataSource = bs;
                }
                catch
                {

                    MessageBox.Show("Bir rapora yalnızca bir adet veri bağlantısı ekleyebilirsiniz!", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void buttonClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Close();
        }

        private void buttonFormatKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.FileName = "Rapor Formatı";
            dlg.Filter = "Rapor Dosyaları|*.repx";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                xrDesignPanel1.Report.SaveLayout(dlg.FileName);
            }
        }

        private void buttonRaporFormatYukle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Rapor Dosyaları|*.repx";
            dlg.Multiselect = false;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                xrDesignPanel1.Report.LoadLayout(dlg.FileName);
            }
        }
    }
}
